<template>
  <view class="content">
    <text>UUID: {{ uuid }}</text>
	
	<!-- <button @click="textUuid">查看</button> -->
	   <!-- 打印机地址输入框 -->
	    <view class="input-box">
	      <input 
	              v-model="printerAddr" 
	              @input="onInputMac"
	              placeholder="请输入打印机地址(12位)" 
	              maxlength="17"  
	            />
	            <text>格式化后: {{ printerAddr }}</text>
	    </view>
	
		<view class="btn-group">
		  <button type="primary" @click="bindPrinter">绑定打印机</button>
		</view>
  </view>
</template>

<script>
	import {bandingMacByUuid}  from  '@/utils/api.js'
export default {
  data() {
    return {
      uuid: '',
	  printerAddr :''
    }
  },
  methods: {
	  // 输入时格式化：大写 + 自动冒号
	      onInputMac(e) {
	        let val = e.detail.value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase(); // 只保留 0-9A-F
	        if (val.length > 12) val = val.slice(0, 12); // 最多 12 位
	        this.printerAddr = val.match(/.{1,2}/g)?.join(":") || val;
	      },
	  // 格式化MAC地址（12位转 A1:B2:C3:D4:E5:F6）
	    formatMac(mac) {
	      if (!mac) return '';
	      // 去掉用户输入的冒号/空格，保持干净
	      mac = mac.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
		  console.log(mac.length)
		  console.log(mac)
	      if (mac.length !== 12) {
	        uni.showToast({ title: "MAC地址必须是12位", icon: "none" });
	        return '';
	      }
	      return mac;
	    },
		// 绑定打印机
		  bindPrinter() {
		    let formattedAddr = this.formatMac(this.printerAddr);
			console.log(formattedAddr)
		    if (!formattedAddr) return;
		
				bandingMacByUuid({printerAddr : this.printerAddr,uuid: this.uuid}).then(res =>{
					console.log(res)
				})
			},
	textUuid(){
		this.uuid = plus.device.uuid;
	}  
  },
  onLoad() {
    // 仅 App-Plus 有效
    if (uni.getSystemInfoSync().platform !== 'mp-weixin') {
      this.uuid = plus.device.uuid;
      console.log('当前设备UUID:', this.uuid);
    }
  }
}
</script>
<style>
	.content {
	  padding: 20rpx;
	}
	.input-box {
	  margin: 20rpx 0;
	}
	.btn-group {
	  display: flex;
	  gap: 20rpx;
	}
</style>
